package services

import (
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"ltbz/cultural_tourism_api/utils"
	"os"
)

const (
	HzEndpoint          = "oss-cn-hangzhou.aliyuncs.com"
	HzBucketName string = "cwdos"

	AccessKeyId     = "LTAI5tE1aZZrP2uzRWBTwgBn"
	AccessKeySecret = "NIwLRBrEjPxOhp2SSRYsFB919uR1lk"
)

// 上传文件到阿里云
func UploadFileToAliyun(filepath, savePath string) error {
	defer func() {
		os.Remove(filepath)
	}()
	var err error
	if OssClient == nil {
		OssClient, err = oss.New(HzEndpoint, AccessKeyId, AccessKeySecret)
		if err != nil {
			utils.FileLog.Info("oss:Upload New err:" + err.Error())
			return err
		}
	}
	bucket, err := OssClient.Bucket(HzBucketName)
	if err != nil {
		utils.FileLog.Info("oss:Bucket err:" + err.Error())
		return err
	}
	err = bucket.PutObjectFromFile(savePath, filepath)
	if err != nil {
		utils.FileLog.Info("oss:PutObjectFromFile err:" + err.Error())
		return err
	}
	return err
}
